<script setup lang="ts" name="Template02">
// import logo from '@/assets/logo.png'
import logo from '@/assets/logo.svg'
import { user } from '@/api/user'
import { getTimeState } from '@/utils/dateFormat'
import { storeToRefs } from 'pinia'
import { useSystemConfigStore } from '@/store/storage/systemConfigStore'
import { useUserInfoStoreWithOut } from '@/store/storage/userInfoStore'
import { transformI18n } from '@/plugins/i18n'
const router = useRouter()
const route = useRoute()
const systemConfigStore = useSystemConfigStore()
const { systemConfig } = storeToRefs(systemConfigStore)
const userInfoStore = useUserInfoStoreWithOut()
const { userInfo } = storeToRefs(userInfoStore)

const loginFormRef = ref(null)

const initInfo = ref({
  login: {
    form: {
      account: 'admin',
      password: '123456',
      code: '8888888'
    },
    rules: {
      account: [{ required: true, message: transformI18n('verifies.' + '请输入账号！', true), trigger: 'blur' }],
      password: [{ required: true, message: transformI18n('verifies.' + '请输入密码！', true), trigger: 'blur' }],
      code: [{ required: true, message: transformI18n('verifies.' + '请输入验证码！', true), trigger: 'blur' }]
    }
  }
})
// 登录点击事件
const onLogin = () => {
  if (!loginFormRef.value) return false
  loginFormRef.value.validate((valid) => {
    if (valid) {
      user.login(initInfo.value.login.form, { config: { mockEnable: true } }).then((res: any) => {
        if (res && res.status) {
          const { accessToken, refreshToken, data } = res
          data.accessToken = accessToken
          data.refreshToken = refreshToken
          userInfo.value = data
          userInfoStore.setUserInfoCache()
          ElNotification({
            title: transformI18n('text.' + getTimeState(), true),
            message: transformI18n('successes.successLoginInfo', true),
            type: 'success'
          })
          systemConfig.value.isLogin = true
          systemConfig.value.isLockScreen = false
          systemConfigStore.setSystemConfigCache()
          // 如果用户真正访问的页面需要登录权限，在跳转到登陆页用户登陆后，需重新跳转到用户真正要访问的页面
          if (route.query.redirect) {
            router.push({ path: route.query.redirect as string })
          } else {
            router.push({ path: '/' })
          }
        }
      })
    }
  })
}
</script>

<template>
  <div class="login-container">
    <div class="login-box">
      <el-icon class="logo-svg" v-motion :initial="{ opacity: 0, y: 100 }" :enter="{ opacity: 1, y: 0, transition: { delay: 100 } }">
        <logo />
      </el-icon>
      <!-- <el-image :src="logo" class="logo-image" v-motion :initial="{ opacity: 0, y: 100 }" :enter="{ opacity: 1, y: 0, transition: { delay: 50 } }"></el-image> -->
      <h2 v-motion :initial="{ opacity: 0, y: 100 }" :enter="{ opacity: 1, y: 0, transition: { delay: 200 } }">
        {{ $t("settings.settingProjectTitle") }}
      </h2>
      <el-form ref="loginFormRef" class="login-form" :model="initInfo.login.form" :rules="initInfo.login.rules">
        <el-form-item prop="account">
          <div class="input-group" v-motion :initial="{ opacity: 0, y: 10 }" :enter="{ opacity: 1, y: 0, transition: { delay: 300 } }">
            <div class="item-icon">
              <el-icon><svg-icon icon="ep:message"></svg-icon></el-icon>
            </div>
            <div class="item-input">
              <el-input type="text" class="input" v-model="initInfo.login.form.account" clearable :placeholder="$t('placeholders.邮箱')" @keyup.enter="onLogin" />
            </div>
          </div>
        </el-form-item>
        <el-form-item prop="password">
          <div class="input-group" v-motion :initial="{ opacity: 0, y: 10 }" :enter="{ opacity: 1, y: 0, transition: { delay: 400 } }">
            <div class="item-icon">
              <el-icon><svg-icon icon="ep:lock"></svg-icon></el-icon>
            </div>
            <div class="item-input">
              <el-input type="password" class="input" v-model="initInfo.login.form.password" show-password clearable :placeholder="$t('placeholders.登录密码')" @keyup.enter="onLogin" />
            </div>
          </div>
        </el-form-item>
        <el-form-item prop="code">
          <div class="input-group" v-motion :initial="{ opacity: 0, y: 10 }" :enter="{ opacity: 1, y: 0, transition: { delay: 500 } }">
            <div class="item-icon">
              <el-icon><svg-icon icon="ri:shield-line"></svg-icon></el-icon>
            </div>
            <div class="item-input">
              <el-input type="text" class="input" v-model="initInfo.login.form.code" clearable :placeholder="$t('placeholders.验证码')" @keyup.enter="onLogin" />
            </div>
          </div>
        </el-form-item>
        <el-button class="btn-login" v-motion :initial="{ opacity: 0, y: 10 }" :enter="{ opacity: 1, y: 0, transition: { delay: 600 } }" @click="onLogin">
          {{ $t("buttons.buttonLogin") }}
        </el-button>
      </el-form>
    </div>
  </div>
</template>

<style scoped lang="scss">
.login-container {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  width: 100vw;
  min-height: 100vh;
  padding: var(--el-space);
  overflow-y: hidden;
  background-color: #2d3a4b;
  background-image: url('../../../../assets/login/template02/login-bg.png');
  background-repeat: no-repeat;
  background-position: 200px center;
}

.login-img {
  display: flex;
  align-items: center;
  justify-content: flex-end;
}

.login-box {
  display: flex;
  flex: 0 0 50%;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;

  .logo-svg {
    color: var(--el-color-primary);
    font-size: 140px;
  }

  .logo-image {
    width: 20%;
    margin: 0 auto;
  }

  h2 {
    margin: 20px 0 0;
    color: #ffffff;
    font-weight: bold;
    font-size: 36px;
    line-height: 1;
    text-align: center;
    text-transform: capitalize;
  }

  .login-form {
    width: 360px;

    .title {
      display: flex;
      align-items: center;

      .el-icon {
        width: 30px;
        height: 30px;
        font-size: 20px !important;
        cursor: pointer;
      }
    }

    :deep(.el-form-item__content) {
      >.el-form-item__error {
        margin-top: 4px;
      }
    }

    .el-form-item {
      margin: 30px 0;

      &.is-error {
        :deep(.el-input__wrapper) {
          box-shadow: none !important;
        }
      }
    }

    >p {
      color: #ffffff;
      font-size: 20px;
      text-align: left;
    }

    .el-input {
      padding: 0;
      background: none;
      border: none;

      :deep(.el-input__wrapper) {
        padding: 0;
        background: none;
        border: none;
        box-shadow: none;

        .el-input__inner {
          padding: 0;
          color: #ffffff;
          font-size: 14px;
          background: none;
          border: none;
        }
      }
    }
  }
}

.input-wrap {
  display: flex;
  flex: 1;

  .input-group {
    margin: 0;
  }

  .item-img {
    display: flex;
    align-items: center;
    height: 42px;
    margin-left: 16px;

    .el-image {
      height: 32px;
    }
  }

  .btn-recover {
    width: auto;
    margin-top: 1px;
    margin-bottom: 1px;
    margin-left: 20px;
    font-size: 14px;
  }
}

.input-group {
  position: relative;
  display: flex;
  flex: 1;
  padding: 5px 0;
  color: #454545;
  background: rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 3px;

  &.focus {

    &::before,
    &::after {
      width: 50%;
    }

    .item-icon {
      color: var(--el-color-primary);
    }

    .item-input {
      h5 {
        top: -5px;
        font-size: 15px;
      }
    }
  }

  .item-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    color: #d9d9d9;
    font-size: 18px;
  }

  .item-input {
    position: relative;
    flex: 1;
    height: 30px;
    padding-right: 15px;

    h5 {
      position: absolute;
      top: 50%;
      left: 10px;
      margin: 0;
      padding: 0;
      color: #d9d9d9;
      font-weight: normal;
      font-size: 16px;
      transform: translateY(-50%);
      transition: 0.3s;
    }

    .el-select {
      display: flex;
      line-height: 30px;

      :deep(.el-input) {
        &.is-focus {
          .el-input__wrapper {
            box-shadow: none !important;
          }
        }
      }

      :deep(.el-input__wrapper) {
        padding: 0;

        &.is-focus {
          box-shadow: none !important;
        }
      }

      :deep(.select-trigger) {
        display: flex;
        flex: 1;

        .el-input__wrapper {
          background-color: transparent;
          box-shadow: none;

          .el-input__inner {
            height: 28px;
            color: #ffffff;
            line-height: 28px;
          }
        }
      }
    }

    .input {
      display: flex;
      width: 100%;
      height: 100%;
      padding: 4px 0;
      color: #ffffff;
      font-size: 16px;
      background: none;
      border: none;
      outline: none;

      &:-webkit-autofill,
      &:-webkit-autofill:hover,
      &:-webkit-autofill:focus,
      &:-webkit-autofill:active {
        transition: color 99999s ease-out 99999s, background-color 99999s ease-out 99999s;
      }
    }
  }
}

.btn-login {
  display: block;
  width: 100%;
  height: 40px;
  margin: 1rem 0;
  color: #ffffff;
  font-size: 16px;
  text-transform: capitalize;
  background: linear-gradient(to right, var(--el-color-primary-light-5), var(--el-color-primary), var(--el-color-primary-light-5));
  background-size: 200%;
  border: none;
  border-radius: 4px;
  outline: none;
  cursor: pointer;
  transition: 0.5s;

  &:hover {
    background-position: right;
  }
}
</style>
